//
//  ResetPasswordViewController.m
//  AI
//
//  Created by zjhc on 2018/1/8.
//  Copyright © 2018年 zjhc. All rights reserved.
//
#import "JKCountDownButton.h"
#import "ResetPasswordViewController.h"
#import "LlbTextField.h"
#import "User.h"
#import "UrlManage.h"
#import "InputPasswordViewController.h"

@interface ResetPasswordViewController ()
@property (weak, nonatomic) IBOutlet JKCountDownButton *smsButton;
@property (weak, nonatomic) IBOutlet UIButton *nextStep;
@property (weak, nonatomic) IBOutlet LlbTextField *phone;
@property (weak, nonatomic) IBOutlet LlbTextField *smsCode;
- (IBAction)smsButtonAction:(id)sender;
@property (weak, nonatomic) IBOutlet UIStackView *smsSentTip;
@property (weak, nonatomic) IBOutlet UILabel *smsSentText;


@end
static NSString *tempData;

@implementation ResetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = NO;
    self.smsButton.layer.cornerRadius = 5.0;
    self.nextStep.layer.cornerRadius = 5.0;
    
    self.phone.horizontalPadding = 15;
    self.smsCode.horizontalPadding = 15;
    self.smsSentTip.hidden = YES;
    tempData = @"";

    
    
}
-(BOOL)mathPhone:(NSString *)phone{
    if (!phone || [phone isEqualToString:@""]) {

      [HXAlertViewEx showInTitle:@"提示" Message:@"请输入手机号" ViewController:self];

        return NO;
    }
    if (![HXNSStringUtil isMobilePhoneNumber:phone]) {

        [HXAlertViewEx showInTitle:@"提示" Message:@"手机号格式不正确,请重新输入" ViewController:self];
        return NO;
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 发送短信验证码按钮
- (IBAction)smsButtonAction:(id)sender {
    
    if (![self mathPhone:self.phone.text]) {
        return;
    }
    
    NSString * str = [NSString stringWithFormat:@"%@****%@",[self.phone.text substringToIndex:3],[self.phone.text substringFromIndex:self.phone.text.length - 4]];
    
    self.smsSentText.text=[NSString stringWithFormat:@"请查看手机%@获取的短信验证码",str];
    self.phone.hidden = YES;
    self.smsSentTip.hidden = NO;
    
    [sender startCountDownWithSecond:10];

    [WTHttpRequest requestJsonPostWithUrlStr:[UrlManage sharedManager].smsSend params:@{@"telphone":self.phone.text} onComplete:^(NSError *error, NSDictionary *resultJson) {
       
        if (!error) {

        } else {
            
            [HXAlertViewEx showInTitle:@"提示" Message:error.domain ViewController:self];
        }
    }];
    
    [sender countDownChanging:^NSString *(JKCountDownButton *countDownButton,NSUInteger second) {
        [countDownButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        NSString *title = [NSString stringWithFormat:@"%zd",second];
        return title;
    }];
    
    [sender countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
        countDownButton.enabled = YES;
        [countDownButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        return @"重新发送";
    }];
}

-(void)viewWillAppear:(BOOL)animated {
    
    self.smsCode.text = @"";
}

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender

{
    
//    if ([self.phone.text isEqualToString:@""]) {
//
//        [HXAlertViewEx showInTitle:@"提示" Message:@"请输入手机号码" ViewController:self];
//
//        return NO;//不执行跳转方法
//
//    }
//
//    if ([self.smsCode.text isEqualToString:@""]) {
//        [HXAlertViewEx showInTitle:@"提示" Message:@"请输入短信验证码" ViewController:self];
//        return NO;
//    }
    
    return YES;//执行跳转方法
    
}

#pragma mark - 页面跳转和传值
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    InputPasswordViewController *inputPwdVC = segue.destinationViewController;
    tempData = self.phone.text;
    inputPwdVC.phone = self.phone.text;
    inputPwdVC.authCode = self.smsCode.text;
    
}

@end
